home *** CD-ROM | disk | FTP | other *** search
- package com.extensibility.xml;
-
- import com.extensibility.util.Debug;
- import java.io.EOFException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.Reader;
- import java.util.Hashtable;
-
- public final class XMLReader extends Reader {
- public static final int UNKNOWN = -1;
- public static final int UTF_8 = 0;
- public static final int UTF_16_1_2 = 1;
- public static final int UTF_16_2_1 = 2;
- public static final int ISO_8859_1 = 3;
- public static final int ASCII = 4;
- // $FF: renamed from: in java.io.InputStream
- InputStream field_0;
- byte[] rawBytes = new byte[8080];
- int pos = 0;
- int eob = 0;
- char nextChar;
- int firstShift;
- int secondShift;
- int encoding = -1;
- String encodingAssignment;
-
- XMLReader(InputStream var1) {
- this.field_0 = var1;
- }
-
- private void establishEncoding() throws IOException {
- if (this.encoding == -1) {
- this.encoding = 0;
-
- try {
- byte var1 = this.getByte();
- byte var2 = this.getByte();
- if (var1 == -2 && var2 == -1) {
- this.encoding = 1;
- this.firstShift = 8;
- } else if (var1 == -1 && var2 == -2) {
- this.encoding = 2;
- this.secondShift = 8;
- } else {
- this.pos = 0;
- }
-
- String var3 = this.extractEncodingDecl();
- if (var3 != null) {
- this.setEncoding(var3);
- }
- } catch (EOFException var4) {
- this.pos = 0;
- }
-
- }
- }
-
- private boolean canReadFromBuffer() {
- return this.pos < this.eob - 4;
- }
-
- private boolean peekStartsWith(String var1) {
- int var2 = this.pos;
- int var3 = 0;
- if (this.eob == -1) {
- return false;
- } else {
- try {
- while(this.canReadFromBuffer()) {
- int var4 = ((Reader)this).read();
- if (var4 != var1.charAt(var3)) {
- boolean var12 = false;
- return var12;
- }
-
- if (var3 == var1.length() - 1) {
- boolean var5 = true;
- return var5;
- }
-
- ++var3;
- }
-
- return false;
- } catch (IOException var10) {
- return false;
- } finally {
- this.pos = var2;
- }
- }
- }
-
- private String peekUntil(String var1) {
- if (this.eob == -1) {
- return null;
- } else {
- int var2 = this.pos;
- int var3 = 0;
- StringBuffer var4 = new StringBuffer();
-
- try {
- while(this.canReadFromBuffer()) {
- int var5 = ((Reader)this).read();
- var4.append((char)var5);
- if (var5 != var1.charAt(var3)) {
- var3 = 0;
- } else if (var3 != var1.length() - 1) {
- ++var3;
- } else {
- String var6 = var4.toString();
- return var6;
- }
- }
-
- return null;
- } catch (IOException var11) {
- return null;
- } finally {
- this.pos = var2;
- }
- }
- }
-
- String extractEncodingDecl() {
- if (!this.peekStartsWith("<?xml")) {
- return null;
- } else {
- String var1 = this.peekUntil("?>");
-
- try {
- Hashtable var2 = XMLStringUtilities.getNameValuePairs(var1.substring(5, var1.length() - 2));
- String var5 = (String)var2.get("encoding");
- return var5;
- } catch (StringIndexOutOfBoundsException var4) {
- Object var3 = null;
- return (String)var3;
- }
- }
- }
-
- public int getEncoding() {
- try {
- if (this.encoding == -1) {
- this.establishEncoding();
- }
- } catch (IOException var2) {
- }
-
- return this.encoding;
- }
-
- public String getAssignedEncoding() {
- try {
- if (this.encoding == -1) {
- this.establishEncoding();
- }
- } catch (IOException var2) {
- }
-
- return this.encodingAssignment;
- }
-
- public static boolean supportsEncoding(String var0) {
- return var0 == null || var0.equalsIgnoreCase("UTF-8") || var0.equalsIgnoreCase("UTF-16") || var0.equalsIgnoreCase("US-ASCII") || var0.equalsIgnoreCase("ASCII") || var0.equalsIgnoreCase("ISO-8859-1");
- }
-
- private boolean setEncoding(String var1) {
- if (var1 == null) {
- this.encoding = 0;
- } else if (var1.equalsIgnoreCase("UTF-8")) {
- this.encoding = 0;
- } else if (var1.equalsIgnoreCase("US-ASCII")) {
- this.encoding = 4;
- } else if (var1.equalsIgnoreCase("ASCII")) {
- this.encoding = 4;
- } else if (var1.equalsIgnoreCase("ISO-8859-1")) {
- this.encoding = 3;
- } else {
- if (!var1.equalsIgnoreCase("UTF-16")) {
- return false;
- }
-
- Debug.assert(this.encoding == 1 || this.encoding == 2, "unexpected encoding");
- }
-
- this.encodingAssignment = var1;
- return true;
- }
-
- public int read(char[] var1, int var2, int var3) throws IOException {
- this.establishEncoding();
- switch (this.encoding) {
- case 0:
- return this.readUTF8(var1, var2, var3);
- case 1:
- case 2:
- return this.readUTF16(var1, var2, var3);
- case 3:
- return this.readISO_8859_1(var1, var2, var3);
- case 4:
- return this.readASCII(var1, var2, var3);
- default:
- return -1;
- }
- }
-
- public int readUTF8(char[] var1, int var2, int var3) throws IOException {
- int var4 = 0;
-
- while(var3-- > 0) {
- if (this.nextChar != 0) {
- var1[var2++] = this.nextChar;
- this.nextChar = 0;
- ++var4;
- } else {
- byte var5;
- try {
- if (this.pos < this.eob) {
- var5 = this.rawBytes[this.pos++];
- } else {
- var5 = this.getByte();
- }
- } catch (EOFException var16) {
- break;
- }
-
- if ((var5 & 128) == 0) {
- var1[var2++] = (char)var5;
- } else if ((var5 & 224) == 192) {
- byte var6;
- try {
- var6 = this.pos < this.eob ? this.rawBytes[this.pos++] : this.getByte();
- } catch (EOFException var15) {
- throw new EncodingException("2-byte character left un-finished.");
- }
-
- if ((var6 & 192) != 128) {
- throw new EncodingException("invalid second byte");
- }
-
- var1[var2++] = (char)((var5 & 31) << 6 | var6 & 63);
- } else if ((var5 & 240) == 224) {
- byte var17;
- try {
- var17 = this.pos < this.eob ? this.rawBytes[this.pos++] : this.getByte();
- } catch (EOFException var14) {
- throw new EncodingException("3-byte character left un-finished (2).");
- }
-
- if ((var17 & 192) != 128) {
- throw new EncodingException("invalid second byte");
- }
-
- byte var7;
- try {
- var7 = this.pos < this.eob ? this.rawBytes[this.pos++] : this.getByte();
- } catch (EOFException var13) {
- throw new EncodingException("3-byte character left un-finished (3).");
- }
-
- if ((var7 & 192) != 128) {
- throw new EncodingException("invalid third byte");
- }
-
- var1[var2++] = (char)((var5 & 15) << 12 | (var17 & 63) << 6 | var7 & 63);
- } else if ((var5 & 248) == 240) {
- byte var18;
- try {
- var18 = this.pos < this.eob ? this.rawBytes[this.pos++] : this.getByte();
- } catch (EOFException var12) {
- throw new EncodingException("4-byte character left un-finished (2).");
- }
-
- if ((var18 & 192) != 128) {
- throw new EncodingException("invalid second byte");
- }
-
- byte var19;
- try {
- var19 = this.pos < this.eob ? this.rawBytes[this.pos++] : this.getByte();
- } catch (EOFException var11) {
- throw new EncodingException("4-byte character left un-finished (3).");
- }
-
- if ((var19 & 192) != 128) {
- throw new EncodingException("invalid third byte");
- }
-
- byte var8;
- try {
- var8 = this.pos < this.eob ? this.rawBytes[this.pos++] : this.getByte();
- } catch (EOFException var10) {
- throw new EncodingException("4-byte character left un-finished (4).");
- }
-
- char var9 = (char)(((15 & var5) << 18) + ((63 & var18) << 12) + ((63 & var19) << 6) + (63 & var8));
- if (var9 > 65536) {
- var1[var2++] = (char)(var9 - 65536 >> 10 | '\ud800');
- this.nextChar = (char)(var9 - 65536 & 1023 | '\udc00');
- }
- } else {
- var1[var2++] = (char)var5;
- }
-
- ++var4;
- }
- }
-
- return var4 == 0 ? -1 : var4;
- }
-
- public int readUTF16(char[] var1, int var2, int var3) throws IOException {
- int var6;
- for(var6 = 0; var3-- > 0; ++var6) {
- byte var4;
- try {
- var4 = this.getByte();
- } catch (EOFException var9) {
- break;
- }
-
- byte var5;
- try {
- var5 = this.getByte();
- } catch (EOFException var8) {
- throw new EncodingException("An odd number of bytes encountered.");
- }
-
- var1[var2++] = (char)(var4 << this.firstShift | var5 << this.secondShift);
- }
-
- return var6 == 0 ? -1 : var6;
- }
-
- public int readISO_8859_1(char[] var1, int var2, int var3) throws IOException {
- int var5;
- for(var5 = 0; var3-- > 0; ++var5) {
- try {
- byte var4 = this.getByte();
- var1[var2++] = (char)var4;
- } catch (EOFException var7) {
- break;
- }
- }
-
- return var5 == 0 ? -1 : var5;
- }
-
- public int readASCII(char[] var1, int var2, int var3) throws IOException {
- int var5;
- for(var5 = 0; var3-- > 0; ++var5) {
- try {
- byte var4 = this.getByte();
- var1[var2++] = (char)var4;
- } catch (EOFException var7) {
- break;
- }
- }
-
- return var5 == 0 ? -1 : var5;
- }
-
- private byte getByte() throws IOException {
- if (this.pos < this.eob) {
- return this.rawBytes[this.pos++];
- } else if (this.eob == -1) {
- throw new EOFException();
- } else {
- Debug.assert(this.pos == this.eob, String.valueOf(String.valueOf(String.valueOf("pos != eob as expected: ").concat(String.valueOf(this.pos))).concat(String.valueOf(" != "))).concat(String.valueOf(this.eob)));
- this.eob = this.field_0.read(this.rawBytes, 0, this.rawBytes.length);
- if (this.eob == -1) {
- throw new EOFException();
- } else {
- this.pos = 0;
- return this.getByte();
- }
- }
- }
-
- public void close() throws IOException {
- if (this.field_0 != null) {
- this.field_0.close();
- this.field_0 = null;
- }
- }
- }
-